﻿using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.RotationAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.GameObjects.Primitives;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.Waiting
{
    public class WaitingMenu : BaseMenuScreen<MainMenu.Menu>
    {
        private readonly ImageContainer _faceLeft;
        private readonly ImageContainer _faceRight;
        private readonly Container _lockContainerLeft;
        private readonly Container _lockContainerRight;

        private readonly Container _panelContainer;
        private readonly TE_Vector2 integerHalfSize;

        public WaitingMenu(TE_Vector2 size)
        {
            integerHalfSize = new TE_Vector2((int) (size.X / 2) + 2, (int) (size.Y / 2) + 2);

            var lockContainer = new Container(size.X / 2, size.Y / 2);

            _lockContainerLeft = new Container(-integerHalfSize.X, 0);
            _lockContainerRight = new Container(integerHalfSize.X, 0);
            _lockContainerLeft.AddAnimations(GetMovementAnimation());
            _lockContainerRight.AddAnimations(GetMovementAnimation());

            _lockContainerLeft.AddChildren(new RepeatingImage {
                                                                  PositionGridBound = new TE_Vector2(0, -integerHalfSize.Y),
                                                                  Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.LockRight),
                                                                  Origin = TE_Rectangle.Alignment.CenterRight,
                                                                  Area = new TE_Vector2(1, integerHalfSize.Y * 2)
                                                              });
            _lockContainerLeft.AddChildren(new RectanglePrimitive {
                                                                      Shape = new TE_Rectangle(new TE_Vector2(-integerHalfSize.X, -integerHalfSize.Y),
                                                                                               integerHalfSize.Offset(-1, integerHalfSize.Y)),
                                                                      FillColor = new Color(168, 168, 168)
                                                                  });

            _lockContainerRight.AddChildren(new RepeatingImage {
                                                                   PositionGridBound = new TE_Vector2(0, -integerHalfSize.Y),
                                                                   Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.LockLeft),
                                                                   Origin = TE_Rectangle.Alignment.CenterLeft,
                                                                   Area = new TE_Vector2(1, integerHalfSize.Y * 2)
                                                               });

            _lockContainerRight.AddChildren(new RectanglePrimitive {
                                                                       Shape = new TE_Rectangle(new TE_Vector2(1, -integerHalfSize.Y),
                                                                                                integerHalfSize.Offset(-1, integerHalfSize.Y)),
                                                                       FillColor = new Color(168, 168, 168)
                                                                   });

            _faceLeft = new ImageContainer {
                                               PositionGridBound = new TE_Vector2(-integerHalfSize.X, 0),
                                               Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.FaceLeft),
                                               Origin = TE_Rectangle.Alignment.CenterRight,
                                               Scale = new TE_Vector2(2, 2)
                                           };

            _faceRight = new ImageContainer {
                                                PositionGridBound = new TE_Vector2(integerHalfSize.X, 0),
                                                Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.FaceRight),
                                                Origin = TE_Rectangle.Alignment.CenterLeft,
                                                Scale = new TE_Vector2(2, 2)
                                            };
            _faceLeft.AddAnimations(GetMovementAnimation(), GetRotationAnimation());
            _faceRight.AddAnimations(GetMovementAnimation(), GetRotationAnimation());

            lockContainer.AddChildren(_lockContainerLeft, _lockContainerRight, _faceLeft, _faceRight);


            _panelContainer = new Container(size.X / 2, 0);

            _panelContainer.AddChildren(new RepeatingImage {
                                                               PositionGridBound = new TE_Vector2(-10, -8),
                                                               Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.Chain),
                                                               Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                               Area = new TE_Vector2(0.5f, 8)
                                                           });
            _panelContainer.AddChildren(new RepeatingImage {
                                                               PositionGridBound = new TE_Vector2(10, -7.5f),
                                                               Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.Chain),
                                                               Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                               Area = new TE_Vector2(0.5f, 8)
                                                           });

            _panelContainer.AddChildren(new ImageContainer {
                                                               Sprite = MenuSheet.GetSprite(MenuSheet.WaitingMenu.Panel),
                                                               Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                               Scale = new TE_Vector2(3, 3)
                                                           });
            _panelContainer.AddChildren(new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, "Waiting for", "other players...") {
                                                                                                                                                              PositionGridBound = new TE_Vector2(0, -3),
                                                                                                                                                              TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                                                                                                                                                              Scale = new TE_Vector2(2, 2)
                                                                                                                                                          });
            _panelContainer.AddAnimations(new PositionAnimation(new BounceEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(size.X / 2, 8), 1500, false, 1500));

            AddChildren(lockContainer, _panelContainer);
        }

        public override void AcceptKey(int id) {}

        public override void CancelKey(int id) {}
        public override void DownKey(int id) {}

        public void EndWaiting()
        {
            _faceLeft.AddAnimations(new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(-integerHalfSize.X, 0), 1500, false, 500));
            _lockContainerLeft.AddAnimations(new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(-integerHalfSize.X, 0), 1500, false, 500));

            _faceRight.AddAnimations(new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(integerHalfSize.X, 0), 1500, false, 500));
            _lockContainerRight.AddAnimations(new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(integerHalfSize.X, 0), 1500, false, 500));

            _panelContainer.AddAnimations(new PositionAnimation(new LinearEasing(), new TE_Vector2(_panelContainer.PositionGridBound.X, 0), 1000, false, 500));

            Die(2000);
        }

        public override void LeftKey(int id) {}
        public override void RightKey(int id) {}
        public override void UpKey(int id) {}

        private PositionAnimation GetMovementAnimation()
        {
            return new PositionAnimation(new QuadraticEasing(BaseTrajectory.Easing.EaseOut), TE_Vector2.Zero, 1500, false, 0);
        }

        private RotationAnimation GetRotationAnimation()
        {
            return new RotationAnimation(new LinearEasing(), 1, 750, false, 1500);
        }
    }
}
